package com.xjl.app.controller;

import com.alibaba.fastjson2.JSON;
import com.xjl.app.EntityDTO.MainDTO;
import com.xjl.app.infra.entity.Devices;
import com.xjl.app.infra.entity.DevicesCallBackMessage;
import com.xjl.app.infra.entity.Station;
import com.xjl.app.infra.service.DevicesCallBackMessageService;
import com.xjl.app.infra.service.DevicesService;
import com.xjl.app.infra.service.StationService;
import com.xjl.common.web.Res.Result;
import com.xjl.feign.DTO.AiMessageResDTO;
import com.xjl.feign.controller.AiMessageResController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("main/data")
@RequiredArgsConstructor
public class MainController {
    private final AiMessageResController aiMessageResController;
    private final DevicesCallBackMessageService devicesCallBackMessageService;
    private final StationService stationService;
    private final DevicesService devicesService;

    @GetMapping
    public Result getMessage() {
        Result list = aiMessageResController.getList();
        String data = (String) list.getData();
        List<AiMessageResDTO> aiMessageResDTOS = JSON.parseArray(data, AiMessageResDTO.class);

        List<MainDTO> resList = aiMessageResDTOS.stream().map(i->{
            Devices devices = devicesService.getById(i.getDeviceId());
            Station station = stationService.getById(i.getStationId());
            DevicesCallBackMessage message = devicesCallBackMessageService.getById(i.getMessageId());

            return MainDTO.builder()
                    .stationId(station.getStationId())
                    .deviceId(devices.getDeviceId())
                    .stationName(station.getStationName())
                    .deviceName(devices.getIotName())
                    .createTime(i.getCreateTime())
                    .pictureUri(message.getPicture())
                    .displayPictureUri(i.getResPicturePath())
                    .build();
        }).collect(Collectors.toList());

        return Result.ok(resList);
    }
}
